<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="边坡名称" prop="slopeName">
        <el-select v-model="queryParams.slopeName" placeholder="请选择边坡名称" clearable style="width: 180px;">
          <el-option
            v-for="dict in slopeCodeList"
            :key="dict.value"
            :label="dict.label"
            :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="测点编号" prop="pointerNumber">
        <el-select v-model="queryParams.pointerNumber" placeholder="请选择测点编号" clearable style="width: 180px;">
          <el-option
              v-for="dict in monitorIds"
              :key="dict.value"
              :label="dict.label"
              :value="dict.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['slope:disposal:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['slope:disposal:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['slope:disposal:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['slope:disposal:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table scrollbar-always-on v-loading="loading" :data="disposalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="边坡名称" align="center" prop="slopeName" width="240px" fixed >
        <template #default="scope">
          <dict-tag :options="slopeCodeList" :value="scope.row.slopeName"/>
        </template>
      </el-table-column>
      <el-table-column label="测点编号" align="center" prop="pointerNumber" width="240px" fixed >
        <template #default="scope">
          <dict-tag :options="monitorIds" :value="scope.row.pointerNumber"/>
        </template>
      </el-table-column>
      <el-table-column label="预警时间" align="center" prop="warningTime" width="180" >
        <template #default="scope">
          <span>{{ parseTime(scope.row.warningTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预警等级" align="center" prop="warningLevel" width="120"/>
      <el-table-column label="预警信息" align="center" prop="warningInfo" width="500"/>
      <el-table-column label="处置单位" align="center" prop="deptId" width="180">
        <template #default="scope">
          <dict-tag :options="sys_dept_list" :value="scope.row.deptId"/>
        </template>
      </el-table-column>
      <el-table-column label="处置人员" align="center" prop="userId" />
      <el-table-column label="处置开始时间" align="center" prop="disposalStartTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.disposalStartTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="到达现场时间" align="center" prop="arrivalTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.arrivalTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="处置措施" align="center" prop="disposalMeasures" />
      <el-table-column label="是否疏散人员" align="center" prop="evacuated">
        <template #default="scope">
          <dict-tag :options="yes_no" :value="scope.row.evacuated"/>
        </template>
      </el-table-column>
      <el-table-column label="疏散人数" align="center" prop="evacuatedNumber" />
      <el-table-column label="处置结果" align="center" prop="disposalResult" />
      <el-table-column label="结果评估" align="center" prop="evaluationLevel" />
      <el-table-column label="是否恢复通行" align="center" prop="resumed">
        <template #default="scope">
          <dict-tag :options="yes_no" :value="scope.row.resumed"/>
        </template>
      </el-table-column>
      <el-table-column label="恢复时间" align="center" prop="resumeTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.resumeTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remarks" />
      <el-table-column label="创建人" align="center" prop="createdBy" />
      <el-table-column label="创建时间" align="center" prop="createdTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdTime, '{y}-{m}-{d} {h}:{m}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" >
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['slope:disposal:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['slope:disposal:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改处置记录对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="disposalRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <!-- 左列 -->
          <el-col :span="12">
            <el-form-item label="边坡名称" prop="slopeName">
              <el-select v-model="form.slopeName" placeholder="请选择边坡名称">
                <el-option
                    v-for="dict in slopeCodeList"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="预警时间" prop="warningTime">
              <el-date-picker clearable
                              v-model="form.warningTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择预警时间">
              </el-date-picker>
            </el-form-item>

            <el-form-item label="预警信息" prop="warningInfo">
              <el-input v-model="form.warningInfo" placeholder="请输入预警信息" />
            </el-form-item>

            <el-form-item label="处置单位" prop="deptId">
              <el-select v-model="form.deptId" placeholder="请选择处置单位">
                <el-option
                    v-for="dict in sys_dept_list"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="处置开始时间" prop="disposalStartTime">
              <el-date-picker clearable
                              v-model="form.disposalStartTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择处置开始时间">
              </el-date-picker>
            </el-form-item>

            <el-form-item label="是否疏散人员" prop="evacuated">
              <el-select v-model="form.evacuated" placeholder="请选择是否疏散人员">
                <el-option
                    v-for="dict in yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="处置结果" prop="disposalResult">
              <el-input v-model="form.disposalResult" type="textarea" placeholder="请输入内容" />
            </el-form-item>

            <el-form-item label="是否恢复通行" prop="resumed">
              <el-select v-model="form.resumed" placeholder="请选择是否恢复通行">
                <el-option
                    v-for="dict in yes_no"
                    :key="dict.value"
                    :label="dict.label"
                    :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="备注" prop="remarks">
              <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
            </el-form-item>
          </el-col>

          <!-- 右列 -->
          <el-col :span="12">
            <el-form-item label="测点编号" prop="pointerNumber">
              <el-select v-model="form.pointerNumber" placeholder="请选择测点编号">
                <el-option
                    v-for="dict in monitorIds"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

            <el-form-item label="预警等级" prop="warningLevel">
              <el-input v-model="form.warningLevel" placeholder="请输入预警等级" />
            </el-form-item>

            <el-form-item label="处置人员" prop="userId">
              <el-input v-model="form.userId" placeholder="请输入处置人员" />
            </el-form-item>

            <el-form-item label="到达现场时间" prop="arrivalTime">
              <el-date-picker clearable
                              v-model="form.arrivalTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择到达现场时间">
              </el-date-picker>
            </el-form-item>

            <el-form-item label="处置措施" prop="disposalMeasures">
              <el-input v-model="form.disposalMeasures" type="textarea" placeholder="请输入内容" />
            </el-form-item>

            <el-form-item label="疏散人数" prop="evacuatedNumber">
              <el-input v-model="form.evacuatedNumber" placeholder="请输入疏散人数" />
            </el-form-item>

            <el-form-item label="结果评估" prop="evaluationLevel">
              <el-input v-model="form.evaluationLevel" placeholder="请输入结果评估" />
            </el-form-item>

            <el-form-item label="恢复时间" prop="resumeTime">
              <el-date-picker clearable
                              v-model="form.resumeTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择恢复时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

  </div>
</template>

<script setup name="Disposal">
import { listDisposal, getDisposal, delDisposal, addDisposal, updateDisposal } from "@/api/slope/disposal";
import {listSlopeCode} from "@/api/slope/info/info.js";
import {listMonitorPoints} from "@/api/slope/monitorPoints/monitorpoints.js";

const { proxy } = getCurrentInstance();
const { sys_dept_list, yes_no } = proxy.useDict('sys_dept_list', 'yes_no');

const monitorIds = ref([])
const slopeCodeList = ref([])
const disposalList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);

const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    slopeName: null,
    pointerNumber: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询处置记录列表 */
function getList() {
  loading.value = true;
  listMonitorPoints().then(response => {
    monitorIds.value = Object.entries(response).map(([key, value]) => ({
      label: key,
      value: value
    }))
  })
  listSlopeCode().then(response => {
    // 假设 response 是一个普通的对象
    slopeCodeList.value = Object.entries(response).map(([name, value]) => ({
      label: name,
      value: name
    }))
  })
  listDisposal(queryParams.value).then(response => {
    disposalList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    slopeName: null,
    pointerNumber: null,
    warningTime: null,
    warningLevel: null,
    warningInfo: null,
    deptId: null,
    userId: null,
    disposalStartTime: null,
    arrivalTime: null,
    disposalMeasures: null,
    evacuated: null,
    evacuatedNumber: null,
    disposalResult: null,
    evaluationLevel: null,
    resumed: null,
    resumeTime: null,
    remarks: null,
    createdBy: null,
    createdTime: null
  };
  proxy.resetForm("disposalRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加处置记录";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDisposal(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改处置记录";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["disposalRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDisposal(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDisposal(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除处置记录编号为"' + _ids + '"的数据项？').then(function() {
    return delDisposal(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('slope/disposal/export', {
    ...queryParams.value
  }, `disposal_${new Date().getTime()}.xlsx`)
}

getList();
</script>
